package org.softmed.rest.admin.module;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.softmed.rest.admin.DetailChooser;
import org.softmed.rest.admin.DetailLevelListener;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.interfaces.CreateListener;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.PathChangedListener;
import org.softmed.rest.admin.interfaces.RemoveListener;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.swing.IconManager;

public class ModuleActionPanel extends JPanel implements ActionListener,
		PathChangedListener, SelectionPathChanged {

	EventListener addResourceListener;

	CreateListener createListener;

	int width = 20;
	int height = 20;

	JLabel validateError = new JLabel(IconManager.warning); // OK
	JLabel changed = new JLabel(IconManager.changed); // OK

	JButton create = new JButton(IconManager.newDocument); // OK
	JButton retrieve = new JButton(IconManager.leftArrow); // OK
	JButton save = new JButton(IconManager.save); // OK
	JButton updateFile = new JButton(IconManager.refresh); // OK
	// JButton updateOnServer = new JButton(IconManager.updateToServer);

	// JButton validateConfig = new JButton(IconManager.validate); // OK
	JButton validateFiles = new JButton(IconManager.validate); // OK

	JButton add = new JButton(IconManager.add); // OK
	// JButton remove = new JButton(IconManager.remove); // OK

	FileHandler fileHandler;

	DetailChooser detailChooser = new DetailChooser(true, DetailLevel.MODULE,
			DetailLevel.RESOURCE, DetailLevel.HANDLER, DetailLevel.METHODS,
			DetailLevel.MEDIA_TYPE);

	public ModuleActionPanel() {
		// setBackground(Color.BLACK);
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		setPreferredSize(new Dimension(540, 30));
		setMaximumSize(new Dimension(540, 30));
		setMinimumSize(new Dimension(540, 30));

		addButton(create);
		addButton(retrieve);
		addButton(save);
		addButton(updateFile);

		// addButton(validateConfig);
		// addButton(validateFiles);
		// addButton(updateOnServer);

		// addButton(remove);
		addButton(add);

		// remove.setEnabled(true);
		add.setEnabled(true);
		add(detailChooser);

		add(Box.createHorizontalGlue());

		add(validateError);
		validateError.setVisible(false);
		// add(changed);

		create
				.setToolTipText("Create new XML file on the file browser's currently selected directory");
		retrieve
				.setToolTipText("Get the XML file path from the browser's currently selected XML file");
		save.setToolTipText("Saves current module configuration XML file");
		updateFile
				.setToolTipText("Updates the editor with the currently saved XML module configuration ");
		// updateOnServer
		// .setToolTipText("Forces the module to be updated on the server,
		// changing its REST configuration");

		// validateConfig
		// .setToolTipText("Validates current module configuration XML file");
		validateFiles
				.setToolTipText("Validates current module configuration XML file and checks for the existence of every refered file on the server");

		validateError.setToolTipText("There was an error validating the file.");
		changed
				.setToolTipText("The configuration has changed from the currently stored version.");

		// create.setEnabled(true);
		validateFiles.setEnabled(true);
		// updateOnServer.setEnabled(true);
	}

	protected void addButton(JButton button) {
		resizeButton(button, width, height);
		button.addActionListener(this);
		button.setEnabled(false);
		add(button);
	}

	protected void resizeButton(JButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();

		if (source == add) {
			addResourceListener.listen(this);
		} else if (source == retrieve) {
			fileHandler.retrieveFilePath();
		} else if (source == create) {
			createListener.created();
		}

	}

	public EventListener getAddResourceListener() {
		return addResourceListener;
	}

	public void setAddResourceListener(EventListener addResourceListener) {
		this.addResourceListener = addResourceListener;
	}

	public void selectionPathChanged(boolean directory, String name,
			String parent) {
		retrieve.setEnabled(false);
		create.setEnabled(false);

		if (name == null) {

		} else if (name.endsWith(".xml")) {
			retrieve.setEnabled(true);
		}
		create.setEnabled(true);

	}

	public FileHandler getFileHandler() {
		return fileHandler;
	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
	}

	@Override
	public void pathChanged(String path) {
		save.setEnabled(false);
		// updateOnServer.setEnabled(false);
		updateFile.setEnabled(false);

		if (path.endsWith(".xml")) {
			save.setEnabled(true);
			// updateOnServer.setEnabled(true);
			updateFile.setEnabled(true);
		}

	}

	public CreateListener getCreateListener() {
		return createListener;
	}

	public void setCreateListener(CreateListener createListener) {
		this.createListener = createListener;
	}

	public void setDetailListener(DetailLevelListener listener) {
		detailChooser.setDetailListener(listener);
	}

	public void setDetailLevel(DetailLevel level) {
		detailChooser.setDetailLevel(level);
	}

	public DetailLevel getDetailLevel() {
		return detailChooser.getDetailLevel();
	}

}
